package com.dingpeikeji.quickdeer.modules.priceup.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.priceup.dao.TPriceupDao;
import com.dingpeikeji.quickdeer.modules.priceup.entity.TPriceup;
import com.dingpeikeji.quickdeer.modules.priceup.entity.TPriceupHist;

/**
 * 平台加价Service
 * 
 * @author dingpei
 * @version 2017-03-08
 */
@Service
@Transactional(readOnly = true)
public class TPriceupService extends CrudService<TPriceupDao, TPriceup> {
	@Autowired
	private TPriceupHistService tHistDao;

	public TPriceup get(String id) {
		return super.get(id);
	}

	public List<TPriceup> findList(TPriceup tPriceup) {
		return super.findList(tPriceup);
	}

	public Page<TPriceup> findPage(Page<TPriceup> page, TPriceup tPriceup) {
		return super.findPage(page, tPriceup);
	}

	@Transactional(readOnly = false)
	public void save(TPriceup tPriceup) {
		TPriceup oldup = super.get(tPriceup.getUpid().toString());
		dao.update(tPriceup);
		/* 更新记录 */
		if (tPriceup.getUpid() != null && (oldup.getUpratio() - tPriceup.getUpratio() > 0.0001
				|| oldup.getUpratio() - tPriceup.getUpratio() < -0.0001)) {
			TPriceupHist tHist = new TPriceupHist();
			tHist.setPreratio(oldup.getUpratio());
			tHist.setPostratio(tPriceup.getUpratio());
			tHist.setUpid(tPriceup.getUpid());
			tHist.setCreateBy(UserUtils.getUser());
			tHistDao.save(tHist);
		}
	}

	@Transactional(readOnly = false)
	public void delete(TPriceup tPriceup) {
		super.delete(tPriceup);
	}

}